package week02

// 77. 组合
// https://leetcode-cn.com/problems/combinations/

func combine(n int, k int) [][]int {
	return combine2(n, k, [][]int{}, []int{})
}

func combine2(index, k int, ans [][]int, temp []int) [][]int {
	if len(temp) > k || len(temp)+index < k {
		return ans
	}
	if index == 0 {
		var t = make([]int, len(temp))
		copy(t, temp)	// 防止对temp的操作影响到已经确定好的结果
		ans = append(ans, t)
		return ans
	}

	ans = combine2(index-1, k, ans, temp)
	temp = append(temp, index)
	ans = combine2(index-1, k, ans, temp)
	temp = temp[:len(temp)-1]
	return ans
}